package com.lwei.tallybook.client.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwei.tallybook.base.domain.Account;
import com.lwei.tallybook.base.domain.AccountBook;
import com.lwei.tallybook.base.domain.Dictionary;
import com.lwei.tallybook.client.controller.BaseController;
import com.lwei.tallybook.service.service.AccountBookService;
import com.lwei.tallybook.service.vo.response.BookResponseVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * 账本控制器
 * @author sks.lwei
 */
@Controller
@RequestMapping("/book")
public class BookAction extends BaseController {

    @Resource
    private AccountBookService accountBookService;

    /**
     * 列出当前用户的账本
     */
    @RequestMapping("/list")
    public String list(Model model, Page<AccountBook> page){
        page.setSize(3);
        Account currentAccount = currentUser();
        IPage<BookResponseVO> pages = accountBookService.listAccountBookByOwner(page, currentAccount.getUuid());
        Page<BookResponseVO> bookPage = (Page<BookResponseVO>) pages;
        model.addAttribute("page", bookPage);
        return "client/book/book_my_list";
    }

    /**
     * 列出指定账单的账本
     */
    @RequestMapping("/list-book-by-bill/{uuid}")
    public String listBookByBill(Model model, Page<AccountBook> page, @PathVariable("uuid") String billUuid){
        page.setSize(3);
        IPage<BookResponseVO> pages = accountBookService.listBookByBill(page, billUuid);
        Page<BookResponseVO> bookPage = (Page<BookResponseVO>) pages;
        model.addAttribute("page", bookPage);
        model.addAttribute("uuid", billUuid);
        return "client/book/book_list";
    }

    /**
     * 与当前用户相关的
     * 列出指定账单的账本
     */
    @RequestMapping("/list-book-by-bill-join/{uuid}")
    public String listBookByBillWithMe(Model model, Page<AccountBook> page, @PathVariable("uuid") String billUuid){
        Account account = currentUser();
        page.setSize(3);
        IPage<BookResponseVO> pages = accountBookService.listBookByBillWhitJoin(page, billUuid, account.getUuid());
        Page<BookResponseVO> bookPage = (Page<BookResponseVO>) pages;
        model.addAttribute("page", bookPage);
        model.addAttribute("uuid", billUuid);
        model.addAttribute("type", "join");
        return "client/book/book_list";
    }

    /**
     * 与当前用户相关的
     * 列出指定账单的账本
     */
    @RequestMapping("/list-book-by-join")
    public String listBookWithMe(Model model, Page<AccountBook> page){
        Account account = currentUser();
        page.setSize(3);
        IPage<BookResponseVO> pages = accountBookService.listAccountBookByJoin(page, account.getUuid());
        Page<BookResponseVO> bookPage = (Page<BookResponseVO>) pages;
        model.addAttribute("page", bookPage);
        model.addAttribute("type", "my_join");
        return "client/book/book_list";
    }


    /**
     * 跳转添加book界面
     */
    @RequestMapping("/add")
    public String toAdd(Model model){
        //获取默认开支类型
        List<Dictionary> typeList = dictionaryService.listDictionaryByType("default");
        model.addAttribute("types", typeList);
        model.addAttribute("current", currentUser());
        return "client/add";
    }

    /**
     * 跳转添加book成功界面
     */
    @RequestMapping("/to-success/{uuid}")
    public String toSuccess(Model model, @PathVariable("uuid") String uuid){
        AccountBook book;
        if(StringUtils.isNotBlank(uuid)){
            book = accountBookService.getAccountBookByUuid(uuid);
            model.addAttribute("book", book);

            Account owner = accountService.getAccountByUuid(book.getConsumeOwner());
            model.addAttribute("owner", owner);

            List<Account> accounts = accountBookService.listAccountByBook(uuid);
            model.addAttribute("accounts", accounts);

        }
        return "client/success";
    }

}
